package at.jku.se.pr.quickshopper.activities;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import at.jku.se.pr.quickshopper.zbar.ZBarConstants;
import at.jku.se.pr.quickshopper.zbar.ZBarScannerActivity;

import at.jku.se.pr.quickshopper.R;

/**
 * class which implements the view where you have to log-in with your customer card
 * after clicking on the "Scan starten"-Button the camera starts 
 */
public class LoginActivity extends Activity {
	
	/**
	 * true if customer is logged-in
	 */
	static boolean isLogged = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		Button scanKundenkarte = (Button) findViewById(R.id.scanKundenkarteBtn);
		scanKundenkarte.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, ZBarScannerActivity.class);
				startActivityForResult(intent, Constants.ZBAR_SCANNER_REQUEST);
			}

		});
		
		Button btnIsLogged = (Button) findViewById(R.id.isLogged);
		btnIsLogged.setEnabled(false);
		if (isLogged) {
			btnIsLogged.setEnabled(true);
			scanKundenkarte.setEnabled(false);
		}
		btnIsLogged.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isLogged) {
					Intent i = new Intent(LoginActivity.this, ShoppingActivity.class);
					i.putExtra("from", 1);
					startActivity(i);
				}
			}

		});
		
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		onCreate(null);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		boolean camUnav = false;
		if (resultCode == RESULT_OK) {
	        Log.e("", "gescannter EAN-Barcode: " + data.getStringExtra(ZBarConstants.SCAN_RESULT));
	        Constants.scanResult = Long.parseLong(data.getStringExtra(ZBarConstants.SCAN_RESULT));
	    } else if(resultCode == RESULT_CANCELED) {
	        Toast.makeText(this, "Camera unavailable", Toast.LENGTH_SHORT).show();
	        camUnav = true;
	    }
		
	    if (!camUnav) {
	    	String str = Constants.createJson("kunde_id");
	    	if (str.equalsIgnoreCase("error")) {
				Toast.makeText(this, "Server/Datenbank ist derzeit in Wartung. Sorry!", Toast.LENGTH_SHORT).show();
			} else {
				try {
					// daraus wieder ein JSONObject machen
					JSONObject c = new JSONObject(str);
					
					// ueberpruefen, ob Kunde bereits in DB vorhanden
					if (c.getString("name").equals("Kunde nicht vorhanden")) {
						Toast.makeText(this, "Deine Karten-Nummer konnte nicht gefunden werden!", Toast.LENGTH_SHORT).show();
						onCreate(null);
					} else {
						Toast.makeText(this, "Hallo " + c.getString("name") + "! Zum Scannen bitte rechts oben Kamera-Symbol anklicken", Toast.LENGTH_LONG).show();
						isLogged = true;
						Constants.customer = Constants.scanResult;
						Intent intent = new Intent(this, ShoppingActivity.class);
						intent.putExtra("from", 0);
						startActivity(intent);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
	    }
	}

}
